package com.booksystem.campuslibrarymanagementsystem.controller.user;


import com.booksystem.campuslibrarymanagementsystem.commen.request.userquest.BorrowCheckrequst.BorrowCheck;
import com.booksystem.campuslibrarymanagementsystem.commen.result.Result;
import com.booksystem.campuslibrarymanagementsystem.entity.BorrowApprovalsTable;
import com.booksystem.campuslibrarymanagementsystem.service.BorrowApprovalsTableService;
import com.booksystem.campuslibrarymanagementsystem.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/User")
@Api(tags = "借阅期查看")
@RestController
public class BorrowCheckController {
    @Autowired
    BorrowApprovalsTableService borrowApprovalsTableService;

    @PostMapping("/BorrowCheck")
    @ApiOperation(value = "借阅期查看",notes = "通过账户id,图书id查询借阅表，查看借阅过期时间")
    public Result BorrowCheck(@RequestBody BorrowCheck borrowCheck) {
        Map<String, Object> parsed = JwtUtil.tokenPre(borrowCheck.getToken());
        if (parsed != null) {
            Object identity = parsed.get("identity");
            Object account = parsed.get("account");
            if (identity.equals("user")) {
                int bookid = borrowCheck.getBookid();//图书id
                // 获取所有与该账户ID相关的借阅记录
                List<BorrowApprovalsTable> allBorrows = borrowApprovalsTableService.existsByAccountAndBookidi((String) account, bookid);

                // 验证是否有借阅记录
                if (allBorrows == null || allBorrows.isEmpty()) {
                    return Result.fail("没有找到任何借阅记录");
                }

                // 提取所有借阅记录的过期时间
                List<String> expirationDates = new ArrayList<>();
                for (BorrowApprovalsTable borrow : allBorrows) {
                    if ("已通过".equals(borrow.getApprovalstate())) { // 只考虑状态为"已通过"的借阅记录
                        expirationDates.add(borrow.getExpirationdate().toString());
                    }
                }

                // 如果没有有效的借阅过期时间，则返回失败
                if (expirationDates.isEmpty()) {
                    return Result.fail("没有找到有效的借阅过期时间");
                }

                // 构建包含所有有效借阅过期时间的消息
                StringBuilder messageBuilder = new StringBuilder("借阅过期时间：\n");
                for (int i = 0; i < expirationDates.size(); i++) {
                    String date = expirationDates.get(i);
                    messageBuilder.append("第 ").append(i + 1).append(" 次借阅过期时间：").append(date).append("\n");
                }
                return Result.success(messageBuilder.toString(), expirationDates.size());
            } else {
                return Result.fail("身份权限不足");
            }
        } else {
            return Result.fail("请输入正确token值");
        }
    }
}
